package com.adtech.adms.commons.web.tag;

import java.io.IOException;
import java.io.Writer;
import java.util.Stack;

import org.apache.struts2.components.Component;

import com.adtech.adms.system.pojo.Feature;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * @author Dormin 
 * @since 2011-5-31 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class NavigationComponent extends Component {

	/**
	 * 文字显示
	 */
	private String text;
	/**
	 * 样式
	 */
	private String style;
	/**
	 * 组建id 用于domument
	 */
	private String id;
	/**
	 * 该页面所对应的模块的code用于返回页面的显示值 （此项为必填项）
	 */
	private String moduleCode;

	private String requestPath;
	/**
	 * 连接的css
	 */
	private String linkStyle;
	/**
	 * 连接的Class
	 */
	private String linkClass;
	private Stack<Feature> features;

	private String onClick;

	public Stack<Feature> getFeatures() {
		return features;
	}

	public void setFeatures(Stack<Feature> features) {
		this.features = features;
	}

	public String getOnClick() {
		return onClick;
	}

	public void setOnClick(String onClick) {
		this.onClick = onClick;
	}

	public String getLinkClass() {
		return linkClass;
	}

	public void setLinkClass(String linkClass) {
		this.linkClass = linkClass;
	}

	public String getLinkStyle() {
		return linkStyle;
	}

	public void setLinkStyle(String linkStyle) {
		this.linkStyle = linkStyle;
	}

	public String getRequestPath() {
		return requestPath;
	}

	public void setRequestPath(String requestPath) {
		this.requestPath = requestPath;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getModuleCode() {
		return moduleCode;
	}

	public void setModuleCode(String moduleCode) {
		this.moduleCode = moduleCode;
	}

	public NavigationComponent(ValueStack stack) {
		super(stack);

	}

	@Override
	public boolean start(Writer writer) {
		boolean start = super.start(writer);
		try {
			writer.write(toHTML(style, text, moduleCode, id, requestPath, linkStyle, linkClass));
		} catch (IOException ex) {

		}

		return start;
	}

	/**
	 * 将对应的参数转换成html代码输出一个span的字符串
	 * 
	 * @param style
	 * @param text
	 * @param moducode
	 * @param id
	 * @return
	 */
	protected String toHTML(String style, String text, String moducode, String id, String requestPath,
			String linkStyle, String linkClass) {

		// 判定用户是否自定义了所显示的文本如果是就显示用户自己的内容不做导航
		if (text != null && !text.equals("")) {
			StringBuffer buffer = new StringBuffer();
			buffer.append("<span ");
			if (id != null && !id.equals("")) {
				buffer.append(" id=\"" + id + "\"");
			}
			buffer.append(" style=\"" + style + "\" ");
			buffer.append(">");
			buffer.append(text);
			buffer.append("</span>");
			return buffer.toString();
		} else {
			String resultString = "";
			StringBuffer buffer = new StringBuffer();
			buffer.append("<span ");
			// 判定是否设置了id
			if (id != null && !id.equals("")) {
				buffer.append(" id=\"" + id + "\"");
			}
			// 加入css
			buffer.append(" style=\"" + style + "\" ");
			buffer.append(">");
			// 如果没有填写moduleCode就输出""
			if (moducode == null || moducode.equals("")) {
				buffer.append(resultString);
			} else {
				// 判定是否存在此code
				if (features.size() > 0) {
					while (!features.isEmpty()) {
						Feature module = features.pop();
						if (features.size() == 0) {
							buffer.append(module.getName());
							return buffer.toString();
						}
						if (module.getUrl() != null && !module.getUrl().equals("")) {
							// 判定是否具有函数要是具有则href="#"

							buffer.append("<a");
							if (onClick == null || onClick.equals("")) {
								if (module.getUrl().startsWith("..")) {
									buffer.append(" href=\"" + module.getUrl() + "\"");
								} else {
									if (module.getUrl().startsWith("\\")) {
										requestPath = requestPath.substring(0, requestPath.length() - 1);
									}
									buffer.append(" href=\"" + requestPath + module.getUrl());

									buffer.append("\"");
								}
							} else {
								buffer.append(" href=\"javascript:void(0)\" ");
								buffer.append(" onclick=\"" + onClick + "(" + module.getCode() + ")" + "\" ");
							}

							if (linkStyle != null && !linkStyle.equals("")) {
								buffer.append(" style=\"" + linkStyle + "\" ");
							}
							if (linkClass != null && !linkClass.equals("")) {
								buffer.append(" class=\"" + linkStyle + "\" ");
							}

							buffer.append(">");
							if (module.getName() != null && !module.getName().equals("")) {
								buffer.append(module.getName());
							}
							buffer.append("</a>");
						} else {
							buffer.append(module.getName());
						}
						buffer.append("--->");
					}

				} else {
					buffer.append(resultString);
				}

			}
			buffer.append("</span>");

			return buffer.toString();
		}
	}
}
